import sys
from ConfigParser import RawConfigParser as ConfigParser

from twisted.internet import reactor

from pankakes import logger
from pankakes.factory import  PankakeFactory
from pankakes.logger import log

log("Launching Pankake Chat Server!", logger.INFO)
try:
	settings = ConfigParser()
	settings.read('config.ini')
	port = settings.getint('network','port')
except Exception as err:
	log('%s'%err, logger.ERROR)
	log("Could not identify port, must abort.", logger.ERROR)
	sys.exit()

factory = PankakeFactory()
reactor.listenTCP(port, factory)

try:
	log("Pankake Chat Server ready.", logger.INFO)
	reactor.run()
finally:
	log("Pankake Chat Server has been shut down.", logger.WARNING)
	logger.logfile.flush()
	logger.logfile.close()
